package com.data;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import net.sf.json.JSONObject;

public class WDataConversion {

	public static String JSONObjectToString(JSONObject json,String name)
	{
		try
		{
			if(!json.containsKey(name))
			{
				return "";
			}
			
			if(json.getString("name") == null)
			{
				return "";
			}
			
			return json.getString("name");
		}
		catch(Exception e)
		{
			return "";
		}
		
	}
	
	public static int JSONObjectToInt(JSONObject json,String name)
	{
		try
		{
			if(!json.containsKey(name))
			{
				return 0;
			}
			
			return json.getInt("name");
		}
		catch(Exception e)
		{
			return 0;
		}
		
	}
	
	public static long JSONObjectToLong(JSONObject json,String name)
	{
		try
		{
			if(!json.containsKey(name))
			{
				return 0;
			}
			
			return json.getLong("name");
		}
		catch(Exception e)
		{
			return 0;
		}
		
	}
	
	public static boolean JSONObjectToBoolean(JSONObject json,String name)
	{
		try
		{
			if(!json.containsKey(name))
			{
				return false;
			}
			
			return json.getBoolean("name");
		}
		catch(Exception e)
		{
			return false;
		}
		
	}
	
	public static Object JSONObjectToObject(JSONObject json,String name)
	{
		try
		{
			if(!json.containsKey(name))
			{
				return null;
			}
			
			return json.get("name");
		}
		catch(Exception e)
		{
			return null;
		}
		
	}
	
	
	public static void sort(ArrayList<Integer> list) {
		Collections.sort(list, new Comparator<Integer>() {
			public int compare(Integer o1, Integer o2) {
				// 按代理级别从上到下排序
				if (o1 > o2) {
					return 1;
				} else {
					return -1;
				}
			}

		});
	}

	public static String formateTime(Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.format(date);

	}

	public static int[] stringToIntArr(String s,String splitStr)
	{
		String[] str = s.split(splitStr);
		int[] arr = new int[str.length];
		for(int i = 0;i<str.length;i++)
		{
			arr[i] = Integer.parseInt(str[i]);
		}
		return arr;
	}
	public static float[] stringToFloatArr(String s,String splitStr)
	{
		String[] str = s.split(splitStr);
		float[] arr = new float[str.length];
		for(int i = 0;i<str.length;i++)
		{
			arr[i] = Float.parseFloat(str[i]);
		}
		return arr;
	}
	public static long[] stringToLongArr(String s,String splitStr)
	{
		String[] str = s.split(splitStr);
		long[] arr = new long[str.length];
		for(int i = 0;i<str.length;i++)
		{
			arr[i] = Integer.parseInt(str[i]);
		}
		return arr;
	}
	
	public static String intArrToString(int[] arr,String splitStr)
	{
		String s = "";
		for(int i = 0;i<arr.length;i++)
		{
			s = s+arr[i];
			if(i< arr.length -1)
			{
				s = s+splitStr;
			}
		}
		return s;
	}
	
	
	public static String IntegerListToString(List<Integer> arr,String splitStr)
	{
		String s = "";
		for(int i = 0;i<arr.size();i++)
		{
			s = s+arr.get(i);
			if(i< arr.size() -1)
			{
				s = s+splitStr;
			}
		}
		return s;
	}
	public static String floatArrToString(float[] arr,String splitStr)
	{
		String s = "";
		for(int i = 0;i<arr.length;i++)
		{
			s = s+arr[i];
			if(i< arr.length -1)
			{
				s = s+splitStr;
			}
		}
		return s;
	}
	public static String longArrToString(long[] arr,String splitStr)
	{
		String s = "";
		for(int i = 0;i<arr.length;i++)
		{
			s = s+arr[i];
			if(i< arr.length -1)
			{
				s = s+splitStr;
			}
		}
		return s;
	}
	public static String MapToString(HashMap<Integer,Integer> map, String splitStr){
		String s = "";
		Iterator<Entry<Integer, Integer>> iter = map.entrySet().iterator();
		while(iter.hasNext()){
			Entry<Integer, Integer> entry = iter.next();
			s = s + entry.getKey() + "," + entry.getValue() + splitStr;
		}
		return s;
	}
	
	public static float randFloat(float min,float max,int weishu)
	{
	
		float f = ((float)(Math.random()* (max- min))+min);
		BigDecimal   b  =   new  BigDecimal(f);  
		float   f1   =  b.setScale(weishu,  BigDecimal.ROUND_DOWN).floatValue();  
		return	f1;
	}
	
	public static int randInt(int min,int max)
	{
		return	(int)(Math.random()* (max- min))+min;
	}
	
	public static long randLong(long min,long max)
	{
		return	(long)(Math.random()* (max- min))+min;
	}
	
	public static long quzheng(long fs)
	{
		if(fs>=10000)
		{
			fs = fs/10000*10000;
		}
		else if(fs>=1000)
		{
			fs = fs/1000*1000;
		}
		else if(fs>=100)
		{
			fs = fs/100*100;
		}
		return fs;
	}
}
